package com.vieewer.btdemo.view.brightness

import android.content.Intent
import android.net.Uri
import android.os.Build
import android.os.Bundle
import android.provider.Settings
import android.util.Log
import android.view.KeyEvent
import android.widget.SeekBar
import com.vieewer.btdemo.R
import com.lindroid.androidutilskt.extension.isAutoBrightness
import com.lindroid.androidutilskt.extension.shortToast
import com.lindroid.androidutilskt.extension.systemBrightness
import com.vieewer.btdemo.base.BaseActivity
import com.youngfeng.snake.annotations.EnableDragToClose
import kotlinx.android.synthetic.main.activity_brightness.*

/**
 * @author yjj
 * @date 2019/9/27
 * @function 亮度工具类
 * @Description
 */
private const val RQ_WRITE_SETTINGS = 100

@EnableDragToClose
class BrightnessActivity :  BaseActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_brightness)
        initView()
    }


    private fun initView() {

        tvAutoBright.text = "是否是自动亮度：${if (isAutoBrightness) "是" else "否"}"
        tvSystemBright.text = "系统亮度=$systemBrightness"
        //sbSystemBright.progress = ((systemBrightness.toFloat() / 255) * 100).toInt()
        sbSystemBright.progress = (systemBrightness.toFloat()).toInt()
        sbSystemBright.max = 255

        //修改系统屏幕亮度需要修改系统设置的权限
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            //如果当前平台版本大于23平台
            if (!Settings.System.canWrite(this)) {
                val intent = with(Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS)) {
                    data = Uri.parse("package:$packageName")
//                    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                    this
                }
                startActivityForResult(intent, RQ_WRITE_SETTINGS)
            } else {
                changeSystemBrightness()
            }
        } else {
            //Android6.0以下的系统则直接修改亮度
            changeSystemBrightness()
        }
    }

    private fun changeSystemBrightness() {
        sbSystemBright.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                systemBrightness = progress
                tvSystemBright.text = "系统亮度=$systemBrightness"
            }


            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }


            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }

        })
    }



    override fun onKeyUp(keyCode: Int, event: KeyEvent?): Boolean {

        Log.i("yjj", "TravelActivity onKeyUp >>>keyCode" + keyCode + ", action =" + event!!.action)

        when(keyCode) {


            22 -> { //往前滑动触控板 +
                Log.i("yjj", "TravelActivity 触控长按松开对应键值为22")
                //var curProgress = sbSystemBright.progress
        /*        if (curProgress <= 85) {
                    sbSystemBright.progress = curProgress + 15
                } else {
                    sbSystemBright.progress = 100
                }*/

                Log.i("add >>>> 111", sbSystemBright.progress.toString())

                sbSystemBright.progress = sbSystemBright.progress + 15

                Log.i("add >>>> 222", sbSystemBright.progress.toString())

            }

            21 -> {  //往后滑动触控板 -
                Log.i("yjj", "TravelActivity 触控长按松开对应键值为21")
               // var curProgress = sbSystemBright.progress
            /*    if (curProgress > 15) {
                    sbSystemBright.progress = curProgress - 15
                } else {
                    sbSystemBright.progress = 0
                }
*/
                Log.i("reduce >>>> 111", sbSystemBright.progress.toString())

                sbSystemBright.progress = sbSystemBright.progress - 15

                Log.i("reduce >>>> 222", sbSystemBright.progress.toString())

            }

        }

        return super.onKeyUp(keyCode, event)
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            RQ_WRITE_SETTINGS -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    if (Settings.System.canWrite(this)) {
                        shortToast("已获取权限")
                        changeSystemBrightness()
                    } else {
                        shortToast("你拒绝了权限")
                    }
                }
            }
        }
    }

}
